package me.magicall.poker;

import java.util.Comparator;

import me.magicall.tagInterfaces.HasNameGetter;
import me.magicall.util.kit.Kits;

public enum Card implements HasNameGetter {

	YELLOW_JOKER(Suit.JOKERS, null) {
		@Override
		public String toString() {
			return "JOKER";
		}
	}, //
	BLACK_JOKER(Suit.JOKERS, null) {
		@Override
		public String toString() {
			return "JOKER";
		}
	},

	SPADES_A, HEARTS_A, CLUBS_A, DIAMONDS_A, //
	SPADES_2, HEARTS_2, CLUBS_2, DIAMONDS_2, //
	SPADES_3, HEARTS_3, CLUBS_3, DIAMONDS_3, //
	SPADES_4, HEARTS_4, CLUBS_4, DIAMONDS_4, //
	SPADES_5, HEARTS_5, CLUBS_5, DIAMONDS_5, //
	SPADES_6, HEARTS_6, CLUBS_6, DIAMONDS_6, //
	SPADES_7, HEARTS_7, CLUBS_7, DIAMONDS_7, //
	SPADES_8, HEARTS_8, CLUBS_8, DIAMONDS_8, //
	SPADES_9, HEARTS_9, CLUBS_9, DIAMONDS_9, //
	SPADES_10, HEARTS_10, CLUBS_10, DIAMONDS_10, //
	SPADES_J, HEARTS_J, CLUBS_J, DIAMONDS_J, //
	SPADES_Q, HEARTS_Q, CLUBS_Q, DIAMONDS_Q, //
	SPADES_K, HEARTS_K, CLUBS_K, DIAMONDS_K, //
	;

	public static final Comparator<Card> A_GREATER_COMPARATOR = new Comparator<Card>() {
		@Override
		public int compare(final Card o1, final Card o2) {
			if (o1 == YELLOW_JOKER) {
				return o2 == YELLOW_JOKER ? 0 : 1;
			} else if (o2 == YELLOW_JOKER) {
				return -1;
			} else if (o1 == BLACK_JOKER) {
				return o2 == BLACK_JOKER ? 0 : 1;
			} else if (o2 == BLACK_JOKER) {
				return -1;
			} else {
				return o1.num.compareTo(o2.num);
			}
		}
	};

	public static Card[] cardsWithoutJokers() {
		return new Card[] { SPADES_A, HEARTS_A, CLUBS_A, DIAMONDS_A, //
				SPADES_2, HEARTS_2, CLUBS_2, DIAMONDS_2, //
				SPADES_3, HEARTS_3, CLUBS_3, DIAMONDS_3, //
				SPADES_4, HEARTS_4, CLUBS_4, DIAMONDS_4, //
				SPADES_5, HEARTS_5, CLUBS_5, DIAMONDS_5, //
				SPADES_6, HEARTS_6, CLUBS_6, DIAMONDS_6, //
				SPADES_7, HEARTS_7, CLUBS_7, DIAMONDS_7, //
				SPADES_8, HEARTS_8, CLUBS_8, DIAMONDS_8, //
				SPADES_9, HEARTS_9, CLUBS_9, DIAMONDS_9, //
				SPADES_10, HEARTS_10, CLUBS_10, DIAMONDS_10, //
				SPADES_J, HEARTS_J, CLUBS_J, DIAMONDS_J, //
				SPADES_Q, HEARTS_Q, CLUBS_Q, DIAMONDS_Q, //
				SPADES_K, HEARTS_K, CLUBS_K, DIAMONDS_K };
	}

	public final Suit suit;
	public final Num num;

	private Card() {
		final String n = name();
		final int index = n.indexOf('_');
		suit = Suit.valueOf(n.substring(0, index));
		final String numStr = n.substring(index + 1);
		final Integer i = Kits.INT.fromString(numStr);
		num = Num.valueOf(i == null ? numStr : "_" + numStr);
	}

	private Card(final Suit suit, final Num num) {
		this.suit = suit;
		this.num = num;
	}

	public Suit getSuit() {
		return suit;
	}

	public Num getNum() {
		return num;
	}

	@Override
	public String toString() {
		return suit.getSign() + num.getName();
	}

	@Override
	public String getName() {
		return name();
	}

	public Color getColor() {
		return suit.getColor();
	}

	public static void main(final String[] args) {
		for (final Card c : Card.values()) {
			System.out.println("@@@@@@Card.main():" + c);
		}
	}
}
